﻿/* 
 *  <copyright file="Logger.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using Microsoft.Xna.Framework;
using SobrietyEngine.Components;
using SobrietyEngine.Events;
using SobrietyEngine.Events.Logging;

namespace SobrietyEngine.Logging
{
    public class Logger:GameComponent, ILoggerGameService
    {
        public static class LogType
        {
            static public int None = 0;
            static public int VSConsole = 1;
            static public int RaiseLogEvent = 2;
            static public int ExceptionOnError = 4;
            static public int AssertOnError = 8;
        }

        //singleton
        static private Logger Singleton;
        public int LogSettings { get; set; }

        public Logger(Microsoft.Xna.Framework.Game game)
            : base(game)
        {
            if (game != null)      
                game.Services.AddService(typeof(ILoggerGameService), this);

            Singleton = this;

            LogSettings = 0;
            //LogSettings = LogType.VSConsole|LogType.ExceptionOnError|LogType.VSConsole|LogType.RaiseLogEvent;
            //LogSettings = LogType.VSConsole | LogType.AssertOnError;
            //if(gameServices!=null)
            //    LogSettings |= LogType.RaiseLogEvent;
        }

        public static Logger GetSingleton()
        {
            if (Singleton == null)
            {
                System.Console.WriteLine("LoggerGameService::Singleton is null!");
                //assume we didnt create this in gameServices, so create a new singleton without 
                //gamengine helpers
                Singleton = new Logger(null);
                return Singleton;
                //throw new Exception("Singleton is null");
            }
            return Singleton;
        }

        IEventRouter EventRouter;

        public override void Initialize()
        {
            base.Initialize();
            EventRouter = (EventRouterGameService)Game.Services.GetService(typeof(IEventRouter));
        }

        public static void AppendLoggingType(int type)
        {
            GetSingleton().LogSettings |= type;
        }

        public static void SetLoggingType(int type)
        {
            GetSingleton().LogSettings = type;
        }

        public static void Error(IComponent cmp, String line)
        {
            GetSingleton().WriteError(cmp, line);
        }

        public static void Error(String line)
        {
            GetSingleton().WriteError(line);
        }

        public void WriteError(IComponent cmp, String line)
        {
            WriteError(cmp.GetType().ToString() + "->" + line);
        }

        public void WriteError(String line)
        {
            WriteLine(line, LogMessageTypes.Errors);
        }

        public static void Debug(String line)
        {
            GetSingleton().WriteDebug(line);
        }

        public static void Debug(IComponent cmp, String line)
        {
            GetSingleton().WriteDebug(cmp, line);
        }

        public void WriteDebug(IComponent cmp, String line)
        {
            WriteDebug(cmp.GetType().ToString() + "->" + line);
        }

        public void WriteDebug(String line)
        {
            WriteLine(line, LogMessageTypes.Debugging);
        }

        public void WriteLine(String line, String Type)
        {
            if (LogSettings == LogType.None)
                return;

            if( (LogSettings & LogType.VSConsole) != 0)
                System.Console.WriteLine(Type + ": " + line);

            if ((LogSettings & LogType.ExceptionOnError) != 0)
                if (Type == LogMessageTypes.Errors)
                    throw new Exception(line);

            if ((LogSettings & LogType.AssertOnError) != 0)
                if (Type == LogMessageTypes.Errors)
                    System.Diagnostics.Debug.Assert(false, line);
            

            if ((LogSettings & LogType.RaiseLogEvent) != 0)
            {
                LoggedItem lied = new LoggedItem(line, Type);
                EventRouter.RaiseEvent(this, lied);
            }
        }

        public override void Update(GameTime gameTime)
        {
        }

    }
}
